﻿/**
* @file parzendensityestimation.h
* @author 川西康友
* @date Last Change: 2011/Feb/24.
* */
#ifndef __BGS_PARZEN_DENSITY_ESTIMATION_H_
#define __BGS_PARZEN_DENSITY_ESTIMATION_H_
#include <cv.h>
#include <deque>
#include <vector>
#include <KernelFunction.h>
/**
* @class BackgroundSubtractorParzen
* @brief Parzen推定によって背景モデルを更新し，背景差分を行う手法
* */
template <class KernelFunction=KernelFunctionEpanechnikov>
class BackgroundSubtractorParzenDensityEstimation{
public:
	BackgroundSubtractorParzenDensityEstimation(const KernelFunction& kf=KernelFunction(), int kernelwidth=25, int capacity=20, double threshold=0.1);
	~BackgroundSubtractorParzenDensityEstimation();
	BackgroundSubtractorParzenDensityEstimation(const BackgroundSubtractorParzenDensityEstimation& other);
	const BackgroundSubtractorParzenDensityEstimation& operator=(const BackgroundSubtractorParzenDensityEstimation& other);

	double getThreshold()const;
	void setThreshold(double thresh);

	void update(const cv::Mat& img);
	void train(const std::vector<cv::Mat>& trainingset);
	void apply(const cv::Mat& img, cv::Mat& oimg);

	double probability(int x, int y, const unsigned char* value)const;
	void getProbabilityMap(const cv::Mat& img, cv::Mat map)const;

private:
	bool isInit;
	KernelFunction kernel;
	int kernelwidth;
	unsigned int capacity;
	double threshold;

	std::deque<cv::Mat> images;

	double distance(const unsigned char* lhs, const unsigned char* rhs)const;
};

#include <BackgroundSubtractor/parzendensityestimation_impl.h>
#endif